package com.zmn.plat.admin.controller.product.graphic;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.graphic.ProductGraphic;
import com.zmn.plat.model.entity.product.graphic.ProductGraphicQuery;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.plat.services.interfaces.product.graphic.ProductGraphicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


/**
 * 产品图文基础查询Controller
 *
 * @author yangshuangping
 * @since 2021/12/09 17:41
 */
@Slf4j
@RestController
@RequestMapping("/graphic/")
public class ProductGraphicListController extends PlatBaseController {

    @Resource
    private ProductGraphicService productGraphicService;
    @Resource
    ServProductService servProductService;

    /**
     * 描述: 查询列表
     *
     * @param query 查询实体
     * @return org.springframework.web.servlet.ModelAndView
     * @author yangshuangping
     * @since 2021/12/09 17:41
     */
    @RequestMapping("/list")
    public ModelAndView list(ProductGraphicQuery query) {

        final ServProduct product = servProductService.findByKey(query.getProductId());

        List<ProductGraphic> items = productGraphicService.listPageByQuery(query);

        ModelAndView mv = new ModelAndView("product/graphic/list");
        mv.addObject("items", items);
        mv.addObject("vo", query);
        mv.addObject("servProductVO", product);

        mv.addObject("flag", 7);
        return mv;
    }

    /**
     * 描述: 详情
     *
     * @param graphicId 主键id
     * @return org.springframework.web.servlet.ModelAndView
     * @author yangshuangping
     * @since 2021/12/09 17:41
     */
    @RequestMapping("/addEditView")
    public ModelAndView addEditView(@RequestParam(value = "productId", defaultValue = "0") Integer productId,
                                    @RequestParam(value = "graphicId", defaultValue = "0") Integer graphicId) {

        final ServProduct product = Optional.ofNullable(servProductService.findByKey(productId)).orElse(new ServProduct());
        ProductGraphic productGraphic;
        if (NumberUtil.isNullOrZero(graphicId)) {
            productGraphic = new ProductGraphic();
        } else {
            productGraphic = productGraphicService.findById(graphicId);
        }
        ModelAndView mv = new ModelAndView("product/graphic/detail");
        mv.addObject("productGraphic", productGraphic);
        mv.addObject("servProductVO", product);


        return mv;
    }

}
